package com.wushi.lenovo.asia5b.app_widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.ui.view.BaseTextWatcher;
import com.wushi.lenovo.asia5b.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhangyuncai on 2018/9/28.
 * 加减法view
 */
public class PlusView extends LinearLayout implements View.OnClickListener, View.OnLongClickListener {
    @BindView(R.id.tv_plusview_sub)
    TextView tv_plusview_sub;
    @BindView(R.id.et_plusview_num)
    EditText et_plusview_num;
    @BindView(R.id.tv_plusview_add)
    TextView tv_plusview_add;
    private QtyChangeListener listener;
    private int qty = 0;
    private int minQty = 1;//该商品选购最低数量
    private int doubleNum = 1;//倍数,>1有效

    public void setDoubleNum(int doubleNum) {
        if (doubleNum < 1) {
            doubleNum = 1;
        }
        this.doubleNum = doubleNum;
    }

    public void setListener(QtyChangeListener listener) {
        this.listener = listener;
    }

    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()) {
            case R.id.tv_plusview_sub:
                subOrAdd(-doubleNum);
                break;
            case R.id.tv_plusview_add:
                subOrAdd(doubleNum);
                break;
            default:
                break;
        }
        return false;
    }

    public interface QtyChangeListener {
        void call(int qty);
    }

    public PlusView(Context context) {
        super(context);
        init();
    }

    public PlusView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PlusView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View view = View.inflate(getContext(), R.layout.view_plus, this);
        ButterKnife.bind(view, this);
        et_plusview_num.setFocusableInTouchMode(true);
        et_plusview_num.setFocusable(true);

        initEvent();
    }

    private void initEvent() {
        tv_plusview_sub.setOnClickListener(this);
        tv_plusview_add.setOnClickListener(this);

        et_plusview_num.addTextChangedListener(new BaseTextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {

                int number = 0;
                try {
                    number = Integer.parseInt(editable.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                qty = number;
                if (listener != null) {
                    listener.call(number);
                }
            }
        });
//        tv_plusview_sub.setOnLongClickListener(this);
//        tv_plusview_add.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_plusview_sub:
                subOrAdd(-doubleNum);
                break;
            case R.id.tv_plusview_add:
                subOrAdd(doubleNum);
                break;
            default:
                break;
        }
    }

    private void subOrAdd(int num) {
        int number = 0;
        try {
            number = Integer.parseInt(et_plusview_num.getText().toString());
            number += num;
            if (number % doubleNum != 0) {
                number = number / doubleNum * doubleNum;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (number < minQty) {
//            number = minQty;
//            ToastUtil.showToast(String.format(ResString.get(R.string.shopcar_minqty_notice), minQty));
        }
        if (number < doubleNum) {
            number = doubleNum;
        }
        et_plusview_num.setText(number + "");

//        et_plusview_num.clearFocus();

    }

    public int getQty() {
        if (qty % doubleNum != 0) {
            qty = qty / doubleNum * doubleNum;
        }
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
        et_plusview_num.setText(qty + "");
    }

    public void setMinQty(int minQty) {
        this.minQty = minQty;
    }
}
